﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace Maticsoft.Web
{
    public partial class Search : System.Web.UI.Page
    {
        string _Key;
        protected void Page_Load(object sender, EventArgs e)
        {
            _Key = Request.QueryString["key"];
            BindData();
            GetNav();
            GetSiteInfo();
        }

        private void BindData()
        {
            StringBuilder strBuilder = new StringBuilder();
            BLL.Books bll = new BLL.Books();
            DataSet ds = bll.GetList("bookName like '%" + _Key + "%' and IsActive=1 order by BookId desc");
            int num = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = ds.Tables[0].Rows[i];
                strBuilder.Append("<li " + (num == 1 ? "class=\"gap\"" : "") + "><a href=\"Detail.aspx?channelId=" + GetParentId(int.Parse(dr["CategoryId"].ToString())) + "&classId=" + dr["CategoryId"] + "&id=" + dr["BookId"].ToString() + "\">");
                strBuilder.Append("<img src=\"uploadpic/" + dr["Pic"].ToString() + "\" width=\"326\" height=\"326\" />");
                strBuilder.Append("<div class=\"product\">");
                strBuilder.Append("<span class=\"productTitle\">" + dr["BookName"].ToString() + "</span> <span class=\"salePrice\">CNY " + decimal.Parse(dr["MarketPrice"].ToString()).ToString("f2") + "</span>");
                strBuilder.Append("</div>");
                strBuilder.Append("</a></li>");
                if (num == 2)
                {
                    num = 0;
                }
                else
                {
                    num++;
                }
            }
            if (strBuilder.ToString() == "")
            {
                strBuilder.Append("<li>no record</li>");
            }

            proList.InnerHtml = strBuilder.ToString();
        }

        protected string GetParentId(int classId)
        {
            BLL.Channel chaBll = new BLL.Channel();
            Model.Channel chaModel = chaBll.GetModel(classId);
            return chaModel.ParentId.ToString();
        }

        protected void GetNav()
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("&nbsp; Home > ");
            strBuilder.Append("Search Result");
            nav.InnerHtml = strBuilder.ToString();

            Title = "Search Result";
        }

        protected void GetSiteInfo()
        {
            BLL.WebSiteConfig bll = new BLL.WebSiteConfig();
            Model.WebSiteConfig model = bll.GetModel(1);
            Title += "-" + model.SiteName;
            MetaKeywords = model.KeyWords;
            MetaDescription = model.Description;
        }
    }
}